package com.mediacloud.app.newsmodule.broadcast;

import com.mediacloud.app.model.news.ProgramListItem;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import java.lang.ref.WeakReference;

public class LiveReplayBroadcast extends BroadcastReceiver {

    public static final String PlayAction = "PlayAction";
    public static final String ToggleAction = "ToggleAction";
    public static final String UpdateState = "UpdateState";
    public static final String UpdateChoose = "UpdateChoose";
    public LiveReplayHandle liveReplayHandle;
    public UpdateListState updateListState;
    private WeakReference<ToggleHandle> toggleHandleWeakReference;
    public boolean registed = false;

    public LiveReplayBroadcast() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (PlayAction.equals(intent.getAction())) {
            ProgramListItem item = (ProgramListItem) intent.getSerializableExtra("data");
            if (item != null && liveReplayHandle != null) {
                liveReplayHandle.handleItem(item);
            }

        } else if (ToggleAction.equals(intent.getAction())) {
            ToggleHandle toggleHandle = toggleHandleWeakReference.get();
            if (toggleHandle != null) {
                toggleHandle.toggle();
            }
        } else if (UpdateState.equals(intent.getAction())) {
            int tableIndex = intent.getIntExtra("index", -1);
            if (updateListState != null)
                updateListState.updateListPlayState(tableIndex);
        } else if (UpdateChoose.equals(intent.getAction())) {
            int tableIndex = intent.getIntExtra("index", -1);
            if (updateListState != null)
                updateListState.updateChoosePlayIndex(tableIndex);
        }
    }

    public void setToggleHandle(ToggleHandle toggleHandle) {
        toggleHandleWeakReference = new WeakReference<>(toggleHandle);
    }

    public interface LiveReplayHandle {
        void handleItem(ProgramListItem item);
    }

    public interface UpdateListState {
        void updateListPlayState(int tableIndex);

        void updateChoosePlayIndex(int tableIndex);
    }

    public interface ToggleHandle {
        void toggle();
    }

}